package com.eshop.modules.activity.service.impl;

import java.lang.invoke.SerializedLambda;
import java.io.IOException;
import com.eshop.utils.FileUtil;
import java.util.ArrayList;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.eshop.common.utils.QueryHelpPlus;
import com.eshop.modules.activity.service.dto.StoreCouponIssueDto;
import java.util.LinkedHashMap;
import com.github.pagehelper.PageInfo;
import java.util.Map;
import org.springframework.data.domain.Pageable;
import com.eshop.modules.activity.service.dto.StoreCouponIssueQueryCriteria;
import com.eshop.modules.activity.domain.StoreCouponIssueUser;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import java.util.Iterator;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.List;
import com.eshop.modules.activity.vo.StoreCouponIssueQueryVo;
import com.eshop.enums.CouponEnum;
import com.eshop.api.EshopException;
import cn.hutool.core.util.ObjectUtil;
import com.eshop.modules.activity.service.StoreCouponIssueUserService;
import com.eshop.modules.activity.service.StoreCouponUserService;
import com.eshop.dozer.service.IGenerator;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import com.eshop.modules.activity.service.StoreCouponIssueService;
import com.eshop.modules.activity.domain.StoreCouponIssue;
import com.eshop.modules.activity.service.mapper.StoreCouponIssueMapper;
import com.eshop.common.service.impl.BaseServiceImpl;

@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = { Exception.class })
public class StoreCouponIssueServiceImpl extends BaseServiceImpl<StoreCouponIssueMapper, StoreCouponIssue> implements StoreCouponIssueService
{
    private final IGenerator generator;
    private final StoreCouponIssueMapper storeCouponIssueMapper;
    private final StoreCouponUserService storeCouponUserService;
    private final StoreCouponIssueUserService storeCouponIssueUserService;
    
    public void issueUserCoupon(final Integer id, final Long uid) {
        final StoreCouponIssueQueryVo couponIssueQueryVo = this.storeCouponIssueMapper.selectOne(id);
        if (ObjectUtil.isNull(couponIssueQueryVo)) {
            throw new EshopException("领取的优惠劵已领完或已过期");
        }
        final int count = this.couponCount(id, uid);
        if (count > 0) {
            throw new EshopException("已领取过该优惠劵");
        }
        if (couponIssueQueryVo.getRemainCount() <= 0 && CouponEnum.PERMANENT_0.getValue().equals(couponIssueQueryVo.getIsPermanent())) {
            throw new EshopException("抱歉优惠卷已经领取完了");
        }
        this.storeCouponUserService.addUserCoupon(uid, couponIssueQueryVo.getCid());
        this.storeCouponIssueUserService.addUserIssue(uid, id);
        if (couponIssueQueryVo.getTotalCount() > 0) {
            this.storeCouponIssueMapper.decCount(id);
        }
    }
    
    public List<StoreCouponIssueQueryVo> getCouponList(final int page, final int limit, final Long uid, final Long productId, Integer type) {
        final Page<StoreCouponIssue> pageModel = (Page<StoreCouponIssue>)new Page((long)page, (long)limit);
        if (type == null) {
            type = CouponEnum.TYPE_0.getValue();
        }
        final List<StoreCouponIssueQueryVo> list = this.storeCouponIssueMapper.selecCoupontList(pageModel, type, productId);
        for (final StoreCouponIssueQueryVo couponIssue : list) {
            final int count = this.couponCount(couponIssue.getId(), uid);
            if (count > 0) {
                couponIssue.setIsUse(true);
            }
            else {
                couponIssue.setIsUse(false);
            }
        }
        return list;
    }
    
    private int couponCount(final Integer id, final Long uid) {
        return ((this.storeCouponIssueUserService.lambdaQuery().eq(StoreCouponIssueUser::getUid, uid)).eq(StoreCouponIssueUser::getIssueCouponId, id)).count();
    }
    
    public Map<String, Object> queryAll(final StoreCouponIssueQueryCriteria criteria, final Pageable pageable) {
        this.getPage(pageable);
        final PageInfo<StoreCouponIssue> page = (PageInfo<StoreCouponIssue>)new PageInfo((List)this.queryAll(criteria));
        final Map<String, Object> map = new LinkedHashMap<String, Object>(2);
        map.put("content", this.generator.convert(page.getList(), (Class)StoreCouponIssueDto.class));
        map.put("totalElements", page.getTotal());
        return map;
    }
    
    public List<StoreCouponIssue> queryAll(final StoreCouponIssueQueryCriteria criteria) {
        return (List<StoreCouponIssue>)this.list((Wrapper)QueryHelpPlus.getPredicate(StoreCouponIssue.class, criteria));
    }
    
    public void download(final List<StoreCouponIssueDto> all, final HttpServletResponse response) throws IOException {
        final List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        for (final StoreCouponIssueDto yxStoreCouponIssue : all) {
            final Map<String, Object> map = new LinkedHashMap<String, Object>();
            map.put(" cname", yxStoreCouponIssue.getCname());
            map.put("优惠券ID", yxStoreCouponIssue.getCid());
            map.put("优惠券领取开启时间", yxStoreCouponIssue.getStartTime());
            map.put("优惠券领取结束时间", yxStoreCouponIssue.getEndTime());
            map.put("优惠券领取数量", yxStoreCouponIssue.getTotalCount());
            map.put("优惠券剩余领取数量", yxStoreCouponIssue.getRemainCount());
            map.put("是否无限张数", yxStoreCouponIssue.getIsPermanent());
            map.put("1 正常 0 未开启 -1 已无效", yxStoreCouponIssue.getStatus());
            list.add(map);
        }
        FileUtil.downloadExcel((List)list, response);
    }
    
    public StoreCouponIssueServiceImpl(final IGenerator generator, final StoreCouponIssueMapper storeCouponIssueMapper, final StoreCouponUserService storeCouponUserService, final StoreCouponIssueUserService storeCouponIssueUserService) {
        this.generator = generator;
        this.storeCouponIssueMapper = storeCouponIssueMapper;
        this.storeCouponUserService = storeCouponUserService;
        this.storeCouponIssueUserService = storeCouponIssueUserService;
    }
}
